/**
 * 
 */
package com.gr.staffpm.widget.dialog.feedback;

import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.extensions.ajax.markup.html.modal.ModalWindow;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.gr.staffpm.datatypes.User;
import com.gr.staffpm.feedback.service.FeedbackService;
import com.gr.staffpm.notes.activity.service.IActivityDaoService;
import com.gr.staffpm.security.service.UserService;
import com.gr.staffpm.widget.dialog.feedback.model.RatingModel;


/**
 * @author Graham Rhodes 11 Apr 2011 23:36:20
 */
public class AjaxFeedbackDirectDialog extends ModalWindow {

    @SpringBean(name = "activityService")
    private IActivityDaoService activityDaoService;
    @SpringBean(name = "userService")
    private UserService userService;
    @SpringBean(name = "feedbackService")
    private FeedbackService feedbackService;
    private static final long serialVersionUID = 1L;

    public AjaxFeedbackDirectDialog(String id, List<User> team) {
        super(id);
        setInitialHeight(370);
        setInitialWidth(600);

        setTitle("Please Leave Feedback For User.");

        setContent(new UserFeedbackPanel(getContentId(), team) {

            private static final long serialVersionUID = 1L;

            @Override
            public void onSubmit(AjaxRequestTarget target, RatingModel rating, User user) {
                AjaxFeedbackDirectDialog.this.submit(target, rating, user);
            }

            @Override
            public void onCancel(AjaxRequestTarget target) {
                AjaxFeedbackDirectDialog.this.close(target);
            }

        });
    }

    protected void submit(AjaxRequestTarget target, RatingModel rating, User user) {
        User currentUser = userService.getCurrentUser();
        feedbackService.addFeedback(rating.getComment(), rating.getRating(), user, currentUser);
        activityDaoService.notifyUserOfFeedback(user, currentUser);
        AjaxFeedbackDirectDialog.this.close(target);
    }

}
